$(function () {
  var search = getSearch();
  console.log(search);
  var categoryid = search.categoryid;



  // 根据商品分类id获取商品分类名称
  $.ajax({
    url: 'http://127.0.0.1:9090/api/getcategorybyid',
    type: 'get',
    data: { categoryid: categoryid },
    success: function (info) {
      console.log(info);
      $('.mmm_main .left').html(template('tmp', info));
    }
  })



  var pageCount; //总页数
  var currentPage = 1; //当前页面 默认值为1

  // 根据分类id获取该分类的商品列表并渲染
  function render(page) {
    $.ajax({
      url: 'http://127.0.0.1:9090/api/getproductlist',
      type: 'get',
      data: {
        categoryid: categoryid,
        pageid: page
      },
      success: function (info) {
        console.log(info);
        $('.items ul').html(template('tmp2', info));

        // 处理分页逻辑
        pageCount = Math.ceil(info.totalCount / info.pagesize);
        var str = "";
        for (var i = 0; i < pageCount; i++) {
          if (i + 1 == page) {
            str += '<option value="' + Number(i + 1) + '" selected>' + Number(i + 1) + '/' + Number(pageCount) + '</option>';
          } else {
            str += '<option value="' + Number(i + 1) + '">' + Number(i + 1) + '/' + Number(pageCount) + '</option>';
          }
        }
        $('#selectPage').html(str);
        //点击选择下拉框事件
        $('#selectPage').change(function () {
          var $value = $(this).val();
          currentPage = $value;
          // console.log($value,currentPage);
          render($value);
          console.log($value);
        });
      }
    })
  }
  

  render();


  console.log('currentPage = ' + currentPage);

  //页码切换点击事件
  $('.prev').on('click', function () {
    if (currentPage > 1) {
      currentPage--;
      render(currentPage);
    }
  });
  $('.next').on('click', function () {
    if (currentPage < pageCount) {
      currentPage++;
      render(currentPage);
    }
  });



})